package com.dao;

import com.beans.OrderGoods;
import com.beans.OrderInfo;
import com.beans.PageInfo;
import com.enums.Enums;
import com.jdbc.DBUtil;

import java.text.SimpleDateFormat;
import java.util.List;

public class OrderDao {
    /**
     * 多条件查询订单数量
     * @param orderNo 订单编号
     * @param beginDate 起始日期
     * @param endDate 姐截止日期
     * @param orderState 订单状态
     * @return 成功返回1
     */
    public int getOrderCount(String orderNo, String beginDate, String endDate, String orderState) {
        String sql = "select count(*) from orderInfo where 1=1 ";
        if (orderNo!=""&&orderNo!=null){
            sql += " and orderNo like '%"+orderNo+"%'";
        }
        if (beginDate!=""&&beginDate!=null){
            sql += " and orderDate>='"+beginDate+"'";
        }
        if (endDate!=""&&endDate!=null){
            sql += " and orderDate<='"+endDate+" 23:59:59'";
        }
        if (orderState!=""&&orderState!=null){
            sql += " and orderState='"+ orderState +"'";
        }
        System.out.println(sql);
        Long count = DBUtil.getScalar(sql);
        return new Integer(count+"");
    }

    /**
     * 多条件查询获取订单列表
     * @param orderNo 订单编号
     * @param beginDate 起始时间
     * @param endDate 截止时间
     * @param orderState 订单状态
     * @param page 分页
     * @return 成功返回订单列表
     */
    public List<OrderInfo> getOrderList(String orderNo, String beginDate, String endDate, String orderState, PageInfo page) {
        String sql = "select * from orderInfo where 1=1";

        if (orderNo!=""&&orderNo!=null){
            sql += " and orderNo like '%"+orderNo+"%'";
        }
        if (beginDate!=""&&beginDate!=null){
            sql += " and orderDate>='"+beginDate+"'";
        }
        if (endDate!=""&&endDate!=null){
            sql += " and orderDate<='"+endDate+" 23:59:59'";
        }
        if (orderState!=""&&orderState!=null){
            sql += " and orderState='"+ orderState +"'";
        }
        sql += " order by orderDate desc limit ? , ?";
        System.out.println(sql);
        return DBUtil.getList(sql,OrderInfo.class,page.getBeginRow(),page.getPageSize());
    }

    /**
     * 发货并且更改发货时间
     * @param id 订单编号
     * @return 成功返回1
     */
    public int updateState(int id) {
        String sendDate=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new java.util.Date());
        String sql = "update orderInfo set orderState='"+Enums.EnumOrderState.已发货.getValue()+"',sendDate=? where id=?";
        return DBUtil.update(sql,sendDate,id);
    }

    /**
     * 根据id获取订单信息
     * @param id 订单id
     * @return 订单对象
     */
    public OrderInfo getOrderDetailById(int id) {
        String sql = "select a.*,b.memberNO as memberNO  from orderInfo a left join memberInfo b on a.memberId=b.id  where a.id= ? ";
        return DBUtil.getSingleObj(sql,OrderInfo.class,id);
    }

    /**
     * 根据订单id获取购买的商品列表
     * @param orderId 订单id
     * @return 返回商品列表
     */
    public List<OrderGoods> getGoodsList(int orderId) {
        String sql = "select * from orderGoods where orderId=?";
        return DBUtil.getList(sql,OrderGoods.class,orderId);
    }
}
